<!DOCTYPE html>
<html>

<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>规格管理</title>
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <link rel="stylesheet" href="../plugins/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/AdminLTE.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/skins/_all-skins.min.css">
    <link rel="stylesheet" href="../css/style.css">
	<script src="../plugins/jQuery/jquery-2.2.3.min.js"></script>
    <script src="../plugins/bootstrap/js/bootstrap.min.js"></script>
    
</head>

<body class="hold-transition skin-red sidebar-mini" >
  <!-- .box-body -->                
                    <div class="box-header with-border">
                        <h3 class="box-title">规格管理</h3>
                    </div>
                    <div class="box-body">
                        <!-- 数据表格 -->
                        <div class="table-box">

                            <!--工具栏-->
                            <div class="pull-left">
                                <div class="form-group form-inline">
                                    <div class="btn-group">
                                        <button type="button" class="btn btn-default" title="新建" data-toggle="modal" data-target="#editModal" ><i class="fa fa-file-o"></i> 新建</button>
                                        <button type="button" class="btn btn-default" title="删除" onclick="deleteInfo()" ><i class="fa fa-trash-o"></i> 删除</button>
                                        
                                        <button type="button" class="btn btn-default" title="刷新" onclick="window.location.reload();"><i class="fa fa-refresh"></i> 刷新</button>
                                    </div>
                                </div>
                            </div>
                            <div class="box-tools pull-right">
                                <div class="has-feedback">
							                    规格名称：<input name="specName" id="myName">
									<button class="btn btn-default" onclick="getSpList()">查询</button>
                                </div>
                            </div>
                            <!--工具栏/-->
			                  <!--数据列表-->
			                  <table id="dataList" class="table table-bordered table-striped table-hover dataTable">
			                      <thead>
			                          <tr>
			                              <th class="" style="padding-right:0px">
			                                  <input id="selall" type="checkbox" class="icheckbox_square-blue">
			                              </th> 
										  <th class="sorting_asc">规格ID</th>
									      <th class="sorting">规格名称</th>									     												
					                      <th class="text-center">操作</th>
			                          </tr>
			                      </thead>
			                      <tbody id="sp">

			                      </tbody>
			                  </table>
			                  <!--数据列表/-->    
                        </div>
                        <!-- 数据表格 /--> 
                     </div>
                    <!-- /.box-body -->
  				<!--分页-->
			  <div id="Number"></div>
			  <input type="hidden" id="page" value="1" size="1px">
			  <input type="hidden" id="pages" size="1px">
	          		    
                                
<!-- 增加窗口 -->
<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog" >
	<div class="modal-content">
		<div class="modal-header">
			<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
			<h3 id="myModalLabel">规格增加</h3>
		</div>
		<div class="modal-body">
			<form id="form_add">
			<table class="table table-bordered table-striped"  width="800px">
		      	<tr>
		      		<td>规格名称</td>
		      		<td><input  class="form-control" id="specName" name="specName" placeholder="规格名称" >  </td>
		      	</tr>
			 </table>

				<!-- 规格选项 -->
				<div class="btn-group">
					<button type="button" class="btn btn-default" title="新建"><i class="fa fa-file-o"></i> 新增规格选项
					</button>

				</div>

				<table  class="table table-bordered table-striped table-hover dataTable">
					<thead>
					<tr>


						<th class="sorting">规格选项</th>
						<th class="sorting">排序</th>
						<th class="sorting">操作</th>
					</thead>
					<tbody>
					<tr>

						<td>
							<input class="form-control" name="optionName" placeholder="规格选项">
						</td>
						<td>
							<input class="form-control" name="orders" placeholder="排序">
						</td>
						<td>
							<button type="button" class="btn btn-default" title="删除"><i class="fa fa-trash-o"></i> 删除
							</button>
						</td>
					</tr>
					<tr>

						<td>
							<input class="form-control" name="optionName" placeholder="规格选项">
						</td>
						<td>
							<input class="form-control" name="orders" placeholder="排序">
						</td>
						<td>
							<button type="button" class="btn btn-default" title="删除"><i class="fa fa-trash-o"></i> 删除
							</button>
						</td>
					</tr>
					<tr>

						<td>
							<input class="form-control" name="optionName" placeholder="规格选项">
						</td>
						<td>
							<input class="form-control" name="orders" placeholder="排序">
						</td>
						<td>
							<button type="button" class="btn btn-default" title="删除"><i class="fa fa-trash-o"></i> 删除
							</button>
						</td>
					</tr>
					</tbody>
				</table>
			</form>

		</div>
		<div class="modal-footer">						
			<button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="editModal()">保存</button>
			<button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
		</div>
	  </div>
	</div>
</div>

  <!-- 编辑窗口 -->
  <div class="modal fade" id="updateModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	  <div class="modal-dialog" >
		  <div class="modal-content">
			  <div class="modal-header">
				  <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
				  <h3 id="myModalLabel1">规格编辑</h3>
			  </div>
			  <div class="modal-body">
				  <form id="form_update">
					  <table  id="tables" class="table table-bordered table-striped"  width="800px">
						  <tr>
							  <td>规格名称</td>
							  <td><input  class="form-control" name="specName" placeholder="规格名称" >  </td>
						  </tr>
					  </table>

					  <!-- 规格选项 -->
					  <div class="btn-group">
						  <button type="button" class="btn btn-default" title="新建"><i class="fa fa-file-o"></i> 新增规格选项
						  </button>

					  </div>

					  <table class="table table-bordered table-striped table-hover dataTable">
						  <thead>
						  <tr>


							  <th class="sorting">规格选项</th>
							  <th class="sorting">排序</th>
							  <th class="sorting">操作</th>
						  </thead>
						  <tbody  id="tbody">
						  <tr>

							  <td>
								  <input class="form-control" name="optionName" placeholder="规格选项">
							  </td>
							  <td>
								  <input class="form-control" name="orders" placeholder="排序">
							  </td>
							  <td>
								  <button type="button" class="btn btn-default" title="删除"><i class="fa fa-trash-o"></i> 删除
								  </button>
							  </td>
						  </tr>
						  <tr>

							  <td>
								  <input class="form-control" name="optionName" placeholder="规格选项">
							  </td>
							  <td>
								  <input class="form-control" name="orders" placeholder="排序">
							  </td>
							  <td>
								  <button type="button" class="btn btn-default" title="删除"><i class="fa fa-trash-o"></i> 删除
								  </button>
							  </td>
						  </tr>
						  <tr>

							  <td>
								  <input class="form-control" name="optionName" placeholder="规格选项">
							  </td>
							  <td>
								  <input class="form-control" name="orders" placeholder="排序">
							  </td>
							  <td>
								  <button type="button" class="btn btn-default" title="删除"><i class="fa fa-trash-o"></i> 删除
								  </button>
							  </td>
						  </tr>
						  </tbody>
					  </table>
				  </form>
			  </div>
			  <div class="modal-footer">
				  <button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="updateModal()">保存</button>
				  <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
			  </div>
		  </div>
	  </div>
  </div>

</body>

</html>
<script>

	$(function (){
		getSpList();
	})
	//查询
	function getSpList(){
		var page = parseInt($("#page").val());
		$.ajax({
			url : "/specification/spPage",
			data : {pageNumber : page,specName:$("#myName").val()},
			async : false,
			dataType : "json",
			type : "post",
			success:function (result) {
				var data = result.data.rows;
				var table = '';
				for (var i=0;i<data.length;i++){
					table += '<tr>';
					table += '<td><input type="checkbox" id="box" value="'+data[i].id+'"></td>';
					table += '<td>'+data[i].id+'</td>';
					table += '<td>'+data[i].specName+'</td>';
					table += '<td class="text-center"><td class="text-center">';
					table += '<button type="button" class="btn bg-olive btn-xs" onclick="specUpdate('+data[i].id+')" data-toggle="modal" data-target="#updateModal">修改';
					table += '<button type="button" class="btn bg-olive btn-xs" onclick="specdelete('+data[i].id+')" data-toggle="modal" data-target="#updateModal">删除';
					table += '</button>';
					table += '</td>';
					table += '</tr>';
				}

				$("#sp").html(table);

				//分页
				var num = ''
				num += '<button onclick="toPage(3)">首页</button>';
				num += '<button onclick="toPage(1)">上一页</button>';
				num += '<button onclick="toPage(2)">下一页</button>';
				num += '<button onclick="toPage(4)">末页</button>';
				$("#Number").html(num);
				var d = result.data;
				$("#pages").val(d.pages);
				$("#total").val(d.total);
				$("#pageSize").val(d.pageSize);

			},
			error:function (data) {
				alert("系统异常")
			}
		})
	}
	//分页
	function toPage(sb){

		var page = parseInt($("#page").val());
		var pages = parseInt($("#pages").val());

		if(sb == 1){
			$("#page").val(page - 1);
		}else if(sb == 2){
			if (page!=pages){
				$("#page").val(page + 1);
			}
		}else if(sb == 3){
			$("#page").val(1);
		}else if(sb == 4){
			$("#page").val(pages);
		}
		getSpList()
	}
	//增加
	function editModal(){
		var name = $("#specName").val();
		$.ajax({
			url : "/specification/addModal",
			data : $("#form_add").serialize(),
			async : false,
			dataType : "json",
			type : "post",
			success:function (result) {
				if(result.code == 10000){
					alert("增加成功");
					location.reload();
				}else{
					alert("增加失败")
				}

			},
			error:function (data) {
				alert("系统异常")
			}
		})
	}
	//批量删除
	function deleteInfo() {
		var ids = $("#box:checked");
		var brandIds = [];
		for (var i = 0; i < ids.length; i++) {
			brandIds.push(ids[i].value)
		}
		$.ajax({
			url: "/specification/deleteInfo",
			data: {id: brandIds},
			dataType: "json",
			type: "post",
			async: true,
			success: function (result) {
				if (result.code == 10000) {
					window.location.reload();
					alert(result.message);
				} else {
					alert(result.message);

				}
			},
			error: function (result) {
				alert("系统异常")
			}
		})
	}

	//删除
	function specdelete(id) {
		$.ajax({
			url: "/specification/deleteById",
			data: {id: id},
			dataType: "json",
			type: "post",
			async: true,
			success: function (result) {
				if (result.code == 10000) {
					window.location.reload();
					alert(result.message);
				} else {
					alert(result.message);

				}
			},
			error: function (result) {
				alert("系统异常")
			}
		})
	}
	//回显
	function specUpdate(id){
		$.ajax({
			url : "/specification/getSpecById",
			data : {id:id},
			async : false,
			dataType : "json",
			type : "post",
			success:function (result) {
				var spec = result.data.spec;
				var table = '';
				table += '<tr>';
				table += '<td>规格名称</td>';
				table += '<td><input class="form-control" type="hidden" value="'+spec.id+'" name="id" placeholder="规格id"></td>';
				table += '<td><input class="form-control" value="'+spec.specName+'" name="specName" placeholder="规格名称"></td>';
				table += '</tr>';

				$("#tables").html(table);

				var list = result.data.optionList;
				var tr = '';
				for(var i = 0;i < list.length;i++){
					tr+='<tr>';
					tr+='<td>';
					tr+='<input class="form-control" value="'+list[i].optionName+'" name="optionName" placeholder="规格选项">';
					tr+='</td>';
					tr+='<td>';
					tr+='<input class="form-control" value="'+list[i].orders+'" name="orders" placeholder="排序">';
					tr+='</td></td>';
					tr+='<td>';
					tr+='<button type="button" class="btn btn-default" title="删除"><i class="fa fa-trash-o"></i> 删除';
					tr+='</button>';
					tr+='</td>';
					tr+='</tr>';
				}
				$("#tbody").html(tr);
			},
			error:function (data) {
				alert("系统异常")
			}
		})
	}
	//修改
	function updateModal(){
			$.ajax({
				url : "/specification/updateModal",
				data : $("#form_update").serialize(),
				async : false,
				dataType : "json",
				type : "post",
				success:function (result) {
					if(result.code == 10000){
						alert("修改成功");
						location.reload();
					}else{
						alert("修改失败")
					}

				},
				error:function (data) {
					alert("系统异常")
				}
			})
	}
</script>